package com.hzy.fivepalace.common.config;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.TimeZone;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.autoconfigure.jackson.JacksonProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;

@Slf4j
@Configuration
public class JacksonConfig {

	@Primary
	@Bean
	public ObjectMapper getObjectMapper(Jackson2ObjectMapperBuilder builder, JacksonProperties jacksonProperties) {
		ObjectMapper objectMapper = builder.createXmlMapper(false).build();
		// 全局配置序列化返回 JSON 处理
		SimpleModule simpleModule = new SimpleModule();
		simpleModule.addSerializer(Long.class, BigNumberSerializer.INSTANCE);
		simpleModule.addSerializer(Long.TYPE, BigNumberSerializer.INSTANCE);
		simpleModule.addSerializer(BigInteger.class, BigNumberSerializer.INSTANCE);
		simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(jacksonProperties.getDateFormat());
		simpleModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(formatter));
		simpleModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(formatter));
		objectMapper.registerModule(simpleModule);
		objectMapper.setTimeZone(TimeZone.getDefault());
		log.info("初始化 jackson 配置");
		return objectMapper;
	}

//	@Bean
//	@Primary
//	@Order(Ordered.HIGHEST_PRECEDENCE)
//	public Jackson2ObjectMapperBuilderCustomizer customJackson() {
//		return new Jackson2ObjectMapperBuilderCustomizer() {
//			@Override
//			public void customize(Jackson2ObjectMapperBuilder builder) {
//				builder.serializerByType(LocalDateTime.class,
//						new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//				/*builder.serializerByType(LocalDateTime.class,
//						new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")));*/
//				builder.serializerByType(LocalDate.class,
//						new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//				builder.serializerByType(LocalTime.class,
//						new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
//
//				builder.deserializerByType(LocalDateTime.class,
//						new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
//				/*builder.deserializerByType(LocalDateTime.class,
//						new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")));*/
//				builder.deserializerByType(LocalDate.class,
//						new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//				builder.deserializerByType(LocalTime.class,
//						new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
//				//builder.serializationInclusion(JsonInclude.Include.NON_NULL);
//				builder.failOnUnknownProperties(false);
//				builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//			}
//		};
//	}
}
